<%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>菜单信息管理</title>
     <%@include file="/common/include.jsp"%>
     	<script>
		$(function(){
			$('#menuList').treegrid({
				rownumbers: true,
				animate:true,
				collapsible:true,
				pagination:true,
				url:'admin/menuAction!menuListJSON.action',
				idField:'ids',
				treeField:'names',
				frozenColumns:[[
				    {field:'ck',checkbox:true},
	                {title:'菜单名称',field:'names',width:200,
		                formatter:function(value){
		                	return '<span style="color:red">'+value+'</span>';
		                }
	                }
				]],
				columns:[[
					 {field:'url',title:'菜单URL',width:350,rowspan:2},
					 {field:'orderIds',title:'排序',width:50,rowspan:2},
					 {field:'description',title:'描述说明',width:250,rowspan:2},
					 {field:'code',title:'标识符',width:200,rowspan:2}
				]],
				onBeforeExpand:function(row,param){
					if(row){
						$(this).treegrid('options').url = 'admin/menuAction!menuListJSON.action?menu.ids='+row.ids;
					}else{
						$(this).treegrid('options').url = 'admin/menuAction!menuListJSON.action';
					}
				},
				onClick:function(node){
					$(this).treegrid('toggle', node.target);
					//alert('you dbclick '+node.text);
				},
				toolbar:[{
					text:'新增',
					iconCls:'icon-add',
					handler:function(){
						append();
					}
				},'-',{
					text:'修改',
					iconCls:'icon-edit',
					handler:function(){modify();}
				},'-',{
					text:'上移',
					iconCls:'icon-edit',
					handler:function(){up();}
				},'-',{
					text:'删除',
					iconCls:'icon-remove',
					handler:function(){remove();}
				},'-',{
					text:'查询',
					iconCls:'icon-search',
					handler:function(){
						$('#menuList').treegrid('options').url = 'admin/menuAction!menuListJSON.action?isSearch=true&_query.names='+$('#names').val();
						$('#menuList').treegrid("reload");
					}
				}],
				onLoadSuccess:function(){
					$('.datagrid-toolbar').append($('#searchTable'));
					$('#searchTable').show();
			       //分页工具条----start ----
				   	var menuListPager = $(this).datagrid('getPager');
					if (menuListPager){	
						$(menuListPager).pagination({
							pageList:[5,10,15],
							displayMsg:'第一级{from}-{to},共{total}',
							buttons:[{
								text:'取消选择',
								iconCls:'icon-unselectAll',
								handler:function(){
									 $('#menuList').treegrid('unselectAll');
								}
							}],
							onSelectPage:function(pageNumber, pageSize){
								$(this).pagination('loading');
								$('#menuList').treegrid('options').url = 'admin/menuAction!menuListJSON.action?rows='+pageSize+'&&page='+pageNumber;
								$('#menuList').treegrid('reload');
								$(this).pagination('loaded');
						   }
						});
					};
					//分页工具条----end -----
				},
				onContextMenu: function(e,row){
					e.preventDefault();
					$(this).treegrid('unselectAll');
					$(this).treegrid('select', row.ids);
					$('#contextMenu').menu('show', {
						left: e.pageX,
						top: e.pageY
					});
				}
			});
		});
		function modify(){
		    $('#first_Menu').hide();
		    $('#second_Menu').hide();
		    $('#threed_Menu').hide();
		  //  $('#code_id').attr('readonly','readonly');
			var node = $('#menuList').treegrid('getSelected');
			if (node){
				var pNode = $('#menuList').treegrid('getParent', node.ids);//父级菜单
				if(pNode){
					TPNode = $('#menuList').treegrid('getParent',pNode.ids);//父二级菜单
					if(TPNode)
						 $('#threed_Menu').show();//显示三级菜单图标修改
					else
						$('#second_Menu').show();//显示二级菜单图标修改
				}else{
					$('#first_Menu').show();//显示一级菜单图标修改
				}
				WinOpen(node.ids);
		    }else{
		    	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>请选择一条数据！</b></div>','warning');
		    }
		}
		function up(){
			var node = $('#menuList').treegrid('getSelected');
			if (node){
			   var pNode = $('#menuList').treegrid('getParent', node.ids);
			   if(pNode){
			   	    var TPNode = $('#menuList').treegrid('getParent',pNode.ids);
					var url="admin/menuAction!moveMenu.action";
					$.post(
							url,
							{"menu.ids":node.ids,"parentMenuIds":TPNode?TPNode.ids:0},
							function(data){
								eval('data='+data);  
					        	var state = data.state;
					        	var retMsg = data.resultMsg;
					        	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>'+retMsg+'</b></div>','info');
					        	if(state==true){
					    			if(!TPNode){
						    			$('#menuList').treegrid('options').url = 'admin/menuAction!menuListJSON.action';
					    			}
					    			$('#menuList').treegrid('reload',TPNode?TPNode.ids:null);
					        	}
							}
					);    			
			   }else
				   $.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>根节点不能往上移动！</b></div>','warning');
				   
		    }else{
		    	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>请选择一条数据！</b></div>','warning');
		    }
		}

		function append(){
		   $('#first_Menu').hide();
		   $('#second_Menu').hide();
		   $('#threed_Menu').hide();
		   $('#code_id').attr('readonly',false);
		   var node = $('#menuList').treegrid('getSelected');
		   if (node){//是否选择节点			    
		   		var pNode = $('#menuList').treegrid('getParent', node.ids);//父级菜单
		   		if(pNode){//是否有父节点
		   			TPNode = $('#menuList').treegrid('getParent',pNode.ids);//父二级菜单
		   			if(TPNode){//是否有父二级节点,有则目前为三级节点不允许往下增加节点
		   				$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>系统暂无支持四级及以上菜单！</b></div>','warning');
		   			}else{
		   				$('#threed_Menu').show();//显示三级菜单图标增加
		   				WinOpen();//否则目前为两级节点也许增加节点
		   			}
		   		}else{//否则就是根节点
		   			$('#second_Menu').show();//显示二级菜单图标增加
		   			WinOpen();
		   		}
		   		$('#menuForm').attr('parentMenuIds').value = node.ids;
		   }else{//否则默认就是根节点
			   $('#first_Menu').show();//显示一级菜单图标增加
			   WinOpen();
		   }
		}
		function remove(){
			var node = $('#menuList').treegrid('getSelected');
			if (node){
				var pNode = $('#menuList').treegrid('getParent', node.ids);//父节点
				$.messager.confirm('提示信息', '<div style="padding-top:15px;padding-left:5px"><b>确定删除？</b></div>', function(r){
					if (r){
						var url="admin/menuAction!deleteMenu.action";
						$.post(
								url,
								{"menu.ids":node.ids},
								function(data){
									eval('data='+data);  
						        	var state = data.state;
						        	var retMsg = data.resultMsg;
						        	if(state==true){
						        		$('#menuList').treegrid('remove', node.ids);
						        		if(!pNode)
						        			$('#menuList').treegrid('options').url = 'admin/menuAction!menuListJSON.action';
						    			$('#menuList').treegrid('reload',pNode?pNode.ids:null);
						        	}
						        	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>'+retMsg+'</b></div>','info');
								}
						);
					}
				});

			}else{
		    	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>请选择一条数据！</b></div>','warning');
		    }
		}
		function save(){
			//添加和修改保存
			var pids = $('#menuForm').attr('parentMenuIds');//父节点ID
			var idsVal = $('#menuForm').attr('ids').value;//节点ID
			if(!pids.value)pids.value=-1;//父节点的父节点
			var pidsVal= pids.value;
			var code =$("#code_id").val();
			var ids =$("#ids_id").val();
			//alert(code);
			var data = {"ids":ids,"code":code};
			 $.ajax({
					url: 'admin/menuAction!checkCodeEdit.action',
					data: data,
					type : "POST",
					cache : false, 
					async : true,
				    dataType:'html',
				    success:function(data){
				    	if(data=="true"){
				    		$('#menuForm').form('submit',{
						        onSubmit: function(){     	
						        	return $('#menuForm').form('validate');
						        },
						        success:function(data){
						        	eval('data='+data);
						        	var state = data.state;
						        	var retMsg = data.resultMsg;
						        	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>'+retMsg+'</b></div>','info');
						        	if(state==true){
						        		if(!idsVal&&pidsVal!=-1){
							        		var newNode = [{
							    				ids: data.ids,
							    				names: data.names,
							    				url: data.url,
							    				orderIds: data.orderIds,
							    				description: data.description,
							    				code:data.code
							    			}];
							    			$('#menuList').treegrid('append', {
							    				parent: (pidsVal!=-1?pidsVal:null),
							    				data: newNode
							    			});
						        	    }
						    			if(pidsVal==-1)
						    				$('#menuList').treegrid('options').url = 'admin/menuAction!menuListJSON.action';
						    		    $('#menuList').treegrid('reload',pidsVal!=-1?pidsVal:null);
										$('#menuWindows').window('close');
							        	$('#menuForm').form('clear');
						        	}
						        }
							});
				    	}else if(data="false"){
				    		alert("标识符已经存在！请重新输入");
				    	}
				    },
				    error:function(){ 
				    },
			 });
		
		}
		function WinClose(){
			$('#menuForm').form('clear');
			$('#menuWindows').window('close');
		}
		function WinOpen(ids){
			   $('#menuForm').form('clear');
			   $('#menuWindows').window({draggable:false});
			   $('#menuWindows').window('open');
			   $('#menuWindows').panel('setTitle','增加菜单');
			   if(ids){
				   $('#menuWindows').panel('setTitle','修改菜单');
				   $('#menuForm').form('load', 'admin/menuAction!viewMenu.action?menu.ids='+ids); 
			   }
		       $('#menuForm').show();
		}
	</script>
</head>
<body class="easyui-layout">
  <div id="menuWindows" class="easyui-window" closed="true" iconCls="icon-save" draggable="false" modal="true" resizable="false" maximizable
  ="false" minimizable
  ="false" title="添加菜单" style="width:520px;height:298px;padding:5px;background:#fafafa;">
	 <div region="center" border="false" style="padding:12px; background:#fff; border:1px solid #ccc;">
	 	<%@include file="menuInfo.jsp"%>
     </div>
     <div region="south" border="false" style="text-align:right;height:30px;line-height:30px;">
     	<a class="easyui-linkbutton" icon="icon-save" href="javascript:void(0)" onclick="save()">保存</a>
     	<a class="easyui-linkbutton" icon="icon-back" href="javascript:void(0);" onclick="WinClose()">返回</a>
     </div>
  </div>
  <div region="center" style="padding:5px;" border="false">
	<table id="menuList" fit="true"></table>
	<div id="contextMenu" class="easyui-menu" style="width:120px;">
		<div onclick="append()" iconCls="icon-add">增加下级菜单</div>
		<div onclick="up()" iconCls="icon-edit">上移菜单</div>
		<div onclick="modify()" iconCls="icon-edit">修改菜单</div>
		<div onclick="remove()" iconCls="icon-remove">移除菜单</div>
	</div>
   </div>
	<table id="searchTable" style="display:none" border="0" cellpadding="0">
		<tr>
			<td>
				<B><input type="text" id="names"/></B>
			</td>
			<td>
				<B>菜单名称</B>
			</td>
		<tr>
	</table>
</body>
</html>